package training.project.app.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import training.project.app.domain.Flight;
import training.project.app.domain.FlightClient;
import training.project.app.domain.UserAccount;
import training.project.app.repository.FlightDao;
import training.project.app.service.FlightClientService;
import training.project.app.service.FlightService;
import training.project.app.service.UtilService;

@Service
@Transactional
public class FlightServiceImpl implements FlightService
	{
		@Autowired
		private FlightDao flightRepository;
		@Autowired
		private FlightClientService flightClientService;
		@Autowired
		private UtilService utilService;

		@Override
		public List<Flight> getFlighs()
			{
				List<Flight> flights = flightRepository.findAll();
				return flights;
			}
		public void updateFlight(Flight flight)
			{
				flightRepository.update(flight);
			}
		@Override
		public Flight getFlightById(Integer id)
			{
				return flightRepository.findById(id);
			}

		@Override
		public void increasePrice(Flight flight)
			{
				flightRepository.increasePrice(flight);
			}

		@Override
		public List<Flight> getClientFlighs(UserAccount client)
			{
				List<Flight> flights = new ArrayList<Flight>();
				List<FlightClient> flightsClients = flightClientService
						.getFlightClients(client.getUserId());
				for (int i = 0; i < flightsClients.size(); i++)
					{
						flights.add(flightRepository.findById(flightsClients
								.get(i).getFlight().getFlightId()));
					}
				return flights;
			}

		@Override
		public List<Flight> findFlights(String startPlace, String endPlace)
			{
				List<Flight> flights = flightRepository.findFlighs(startPlace,
						endPlace);
				return flights;
			}

		@Override
		public List<FlightClient> getFlightClients(Integer flightId)
			{
				// TODO Auto-generated method stub
				return null;
			}

		@Override
		public List<Flight> increasePriceCuzVipAndBaggage(List<Flight> flights,
				Boolean vip, Boolean baggage)
			{
				for (Flight flight : flights)
					{
						increasePriceCuzVipAndBaggage(flight, vip, baggage);
					}
				return flights;
			}

		@Override
		public Flight increasePriceCuzVipAndBaggage(Flight flight, Boolean vip,
				Boolean baggage)
			{
				flight.setPrice(flight.getPrice() + flight.getPrice() * 0.2F
						* utilService.BooleanToInt(vip) + flight.getPrice()
						* 0.1F * utilService.BooleanToInt(baggage));
				return flight;
			}

		@Override
		public List<Flight> DoNotShowBookedAndPurchaseFlights(
				List<Flight> flights, Integer userId)
			{
				List <FlightClient> flightClients = flightClientService.getFlightClients(userId);
				for(int i = 0; i < flights.size();i++)
					{
						for(int j = 0; j < flightClients.size();j++)
							{
								if(flights.get(i).getFlightId() == flightClients.get(j).getFlight().getFlightId())
									{
										flights.remove(i);
										i--;
										break;
									}
							}
					}
				return flights;
			}
	}
